package data

import (
	"061/Sgame/db"
	"061/Sgame/server/game/model"
	"log"
	"time"
)

var RoleAttrDao = &roleAttrDao{
	raChan: make(chan *RoleAttribute, 100),
}

type roleAttrDao struct {
	raChan chan *RoleAttribute
}

func (r *roleAttrDao) running() {
	for {
		select {
		case rr := <-r.raChan:
			err := db.GormDB.
				Model(new(RoleAttribute)).
				Where("id=?", rr.Id).
				Select("parent_id", "collect_times", "last_collect_time", "pos_tags").
				Updates(rr).Error
			if err != nil {
				log.Println("RoleResDao update error", err)
			}
		}
	}

}

func init() {
	go RoleAttrDao.running()
}

// RoleAttribute 玩家属性 比如税收征收次数等
type RoleAttribute struct {
	Id              int            `gorm:"column:id;primaryKey;comment:id"`
	RId             int            `gorm:"column:rid;comment:rid"`
	UnionId         int            `gorm:"column:union_id;comment:联盟id"`             //联盟id
	ParentId        int            `gorm:"column:parent_id;comment:上级联盟id"`          //上级联盟id（被沦陷）
	CollectTimes    int8           `gorm:"column:collect_times;comment:征收次数"`        //征收次数
	LastCollectTime time.Time      `gorm:"column:last_collect_time;comment:最后征收的时间"` //最后征收的时间
	PosTags         string         `gorm:"column:pos_tags;comment:位置标记"`             //位置标记
	PosTagArray     []model.PosTag `gorm:"-"`
}

func (r *RoleAttribute) TableName() string {
	return "role_attribute"
}

func (r *RoleAttribute) SyncExecute() {
	RoleAttrDao.raChan <- r
}
